package com.fowo.api.ptt.service.impl;

import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.RException;
import com.fowo.api.ptt.enitty.PrintTemplate;
import com.fowo.api.ptt.enitty.PrintTemplateGroup;
import com.fowo.api.ptt.enitty.PrintTemplateType;
import com.fowo.api.ptt.mapper.PrintTemplateGroupMapper;
import com.fowo.api.ptt.mapper.PrintTemplateMapper;
import com.fowo.api.ptt.mapper.PrintTemplateTypeMapper;
import com.fowo.api.ptt.model.PrintTemplateSearchParamPo;
import com.fowo.api.ptt.service.PrintTemplateService;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 打印模版服务实现
 * @author yl_ls
 */
@Service
public class PrintTemplateServiceImpl implements PrintTemplateService {
    @Resource
    private PrintTemplateGroupMapper groupMapper;
    @Resource
    private PrintTemplateTypeMapper typeMapper;
    @Resource
    private PrintTemplateMapper templateMapper;


    @Override
    public List<PrintTemplateGroup> getAllTemplateGroups() {
        return groupMapper.selectAll();
    }

    @Override
    public List<PrintTemplateGroup> getVisibleTemplateGroups(Long userId) {
        return groupMapper.selectVisibleByUserId(userId);
    }

    @Override
    public PrintTemplateGroup getTemplateGroupById(Long groupId) {
        return groupMapper.selectById(groupId);
    }

    @Override
    public List<PrintTemplateType> getTypesByGroupId(Long groupId) {
        return typeMapper.selectByGroupId(groupId);
    }

    @Override
    public List<PrintTemplateType> getVisibleTypesByGroupId(Long groupId, Long userId) {
        return typeMapper.selectVisibleByGroupIdAndUserId(groupId, userId);
    }

    @Override
    public Page<PrintTemplate> pageSearch(PrintTemplateSearchParamPo searchParamPo) {
        searchParamPo.setDisableDataPremissions(true);
        return templateMapper.selectPageSearch(searchParamPo.toPage(), searchParamPo);
    }

    @Override
    public PrintTemplate getById(Long id) {
        return templateMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(PrintTemplate printTemplate) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            throw new RException("操作者身份必须提供");
        }
        if (printTemplate.getGroupId() == null) {
            throw new RException("必须设置组编号");
        }
        if (printTemplate.getTypeId() == null) {
            throw new RException("必须设置类型编号");
        }
        if (!StringUtils.hasText(printTemplate.getName())) {
            throw new RException("必须设置模型名称");
        }

        Date now = new Date();
        printTemplate.setCreateUser(jwtUserInfo.getUserId());
        printTemplate.setCreateTime(now);
        printTemplate.setLastUpdateUser(jwtUserInfo.getUserId());
        printTemplate.setLastUpdateTime(now);
        printTemplate.setDelFlag(false);

        if (templateMapper.insert(printTemplate) != 1) {
            throw new RException("插入数据失败");
        }
        return printTemplate.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(PrintTemplate printTemplate) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            throw new RException("操作者身份必须提供");
        }
        PrintTemplate rawTemplate = getById(printTemplate.getId());
        if (rawTemplate == null) {
            throw new RException("未找到要更新的打印模版");
        }

        Date now = new Date();
        printTemplate.setLastUpdateUser(jwtUserInfo.getUserId());
        printTemplate.setLastUpdateTime(now);

        if (templateMapper.updateById(printTemplate) != 1) {
            throw new RException("更新数据失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long id) {
        PrintTemplate rawTemplate = getById(id);
        if (rawTemplate == null) {
            throw new RException("未找到要更新的打印模版");
        }
        if ("0".equals(rawTemplate.getAttrs())) {
            throw new RException("未找到要更新的打印模版");
        }
        if (templateMapper.deleteById(id) != 1) {
            throw new RException("删除数据失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setTypeDefaultTemplateId(Long typeId, Long defaultTemplateId) {
        if(!new UpdateChainWrapper<PrintTemplateType>(typeMapper)
                .set("default_template_id", defaultTemplateId)
                .eq("id", typeId)
                .update()) {
            throw new RException("更新失败！");
        }
    }

    @Override
    public PrintTemplateType getTypeById(Long typeId) {
        return typeMapper.selectById(typeId);
    }
}
